package com.yc.clouduser.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.clouduser.entity.User;
import com.yc.clouduser.mapper.UserMapper;
import com.yc.vcode.bean.Result;
import com.yc.vcode.bean.VcodeProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class mobile {

    @Autowired
    UserMapper userMapper;

    @Autowired
    VcodeProperties properties;

    @RequestMapping("mobileLogin")
    Result mobileLogin(String username, String password, HttpSession session){
        if(username == null || username.isBlank()){
            return new Result(-1, "请输入账号", null);
        }
        if(password == null || password.isBlank()){
            return new Result(-2, "请输入密码", null);
        }

        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getName, username);
        String md5 = DigestUtils.md5DigestAsHex(password.getBytes());
        lqw.eq(User::getPwd, md5);
        User user = userMapper.selectOne(lqw);
        if(user == null){
            return new Result(0, "用户或者密码错误", null);
        }else{
            user.setPwd("");
            //存入会话
            session.setAttribute(properties.getSessionKey(), user);
            return new Result(1, "登录成功", user);
        }

    }
}
